﻿@{
    ViewBag.Title = "RuleIndex";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}

<script>
    $(function () {
        InitialPage();
        GetGrid();
    });
    //初始化页面
    function InitialPage() {
        //layout布局
        $('#layout').layout({
            applyDemoStyles: true,
            onresize: function () {
                $(window).resize()
            }
        });
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 141);
            }, 200);
            e.stopPropagation();
        });
    }
    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $("#gridTable");
        $gridTable.jqGrid({
            url: "../../SystemManage/BussinessIndicator/GetRuleList",
            datatype: "json",
            height: $(window).height() - 171,
            autowidth: true,
            colModel: [
                { label: '主键', name: 'Id', hidden: true },
                { label: '编号', name: 'Code', index: 'Code', width: 200, align: 'center', sortable: false },
                { label: '指标名称', name: 'Name', index: 'Name', width: 100, align: 'center', sortable: false },
                { label: '指标排序', name: 'Sort', index: 'Sort', width: 150, align: 'center', sortable: false },
                {
                    label: "指标图标", name: "Icon", index: "Icon", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue) {
                        return !cellvalue ? "" : "<i class=\"" + cellvalue + "\"></i>";
                    }
                },
                { label: '计算公式', name: 'CountFormula', index: 'CountFormula', width: 80, align: 'center', sortable: false },
                { label: '显示格式', name: 'DisFormat', index: 'DisFormat', width: 80, align: 'center', sortable: false },
                {
                    label: "是否在首页显示", name: "AllowShow", index: "AllowShow", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue) {
                        return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                    }
                },
                { label: '指标说明', name: 'Memo', index: 'Memo', width: 80, align: 'center', sortable: false },
                { label: '标识值', name: 'ValueFlag', index: 'ValueFlag', hidden: true },
            ],
            pager: "#gridPager",
            rowNum: 30,
            rowList: [30, 50, 100],
            sortname: 'Sort',
            sortorder: 'desc',
            onSelectRow: function () {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            },
            subGrid: true,
            subGridRowExpanded: function (subgrid_id, row_id) {
                var ValueFlag = JSON.parse($("#gridTable").jqGrid('getRowData', row_id)['ValueFlag']);
                var subgrid_table_id = subgrid_id + "_t";
                var subhtml = "";
                subhtml += '<div><table id="' + subgrid_table_id + '"></table></table></div>';
                $("#" + subgrid_id).html(subhtml);
                $("#" + subgrid_table_id).jqGrid({
                    datatype: 'local',
                    data: ValueFlag,
                    height: 260,
                    autowidth: true,
                    colModel: [
                 { label: "Id", name: "Id", hidden: true },
                  { label: "Flag", name: "Flag", hidden: true },
                 { label: "界限值说明", name: "ValueMemo", index: "Sort", align: "center", sortable: false },
                 { label: "界限值1", name: "MinValue", width: 60, align: "center", sortable: false },
                 { label: "界限值2", name: "MaxValue", width: 100, align: "center", sortable: false },
                 {
                     label: "颜色", name: "Color", width: 100, align: "center", sortable: false,
                     formatter: function (cellvalue, options, rowObject) {
                         return "<div style='width:70px;background-color:" + cellvalue + "'>&nbsp;</div>";
                     }
                 }
                    ],
                    rowNum: "all",
                    caption: "标识值设置",
                    hidegrid: false,
                    shrinkToFit: false,
                    gridview: true
                });
                if (ValueFlag.length > 0) { $("div.unwritten").remove(); }
                for (var i = 0; i < ValueFlag.length; i++) {
                    $("#" + subgrid_table_id).jqGrid('addRowData', i + 1, ValueFlag[i]);
                }
            }
        });
        //查询条件
        $("#queryCondition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#queryCondition .dropdown-text").html(text).attr('data-value', value)
        });
        //查询事件
        $("#btn_Search").click(function () {
            $gridTable.jqGrid('setGridParam', {
                url: "../../SystemManage/BussinessIndicator/GetRuleList",
                postData: {
                    condition: $("#queryCondition").find('.dropdown-text').attr('data-value'),
                    keyword: $("#txt_Keyword").val()
                },
            }).trigger('reloadGrid');
        });
        //查询回车
        $('#txt_Keyword').bind('keypress', function (event) {
            if (event.keyCode == "13") {
                $('#btn_Search').trigger("click");
            }
        });
    }
    //新增
    function btn_add() {
        dialogOpen({
            id: "Form1",
            title: '添加指标规则',
            url: '/SystemManage/BussinessIndicator/RuleForm',
            width: "700px",
            height: "570px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            }
        });
    };
    //编辑
    function btn_edit() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "Form1",
                title: '编辑指标规则',
                url: '/SystemManage/BussinessIndicator/RuleForm?keyValue=' + keyValue,
                width: "700px",
                height: "570px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            });
        }
    }
    //删除
    function btn_delete() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (keyValue) {
            $.RemoveForm({
                url: "../../SystemManage/BussinessIndicator/RemoveRuleForm",
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#gridTable").resetSelection();
                    $("#gridTable").trigger("reloadGrid");
                }
            })
        } else {
            dialogMsg('请选择需要删除的字典！', 0);
        }
    }
    //详细
    function btn_detail() {
        var keyValue = $("#gridTable").jqGridRowValue("ItemDetailId");
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "Detail",
                title: '字典信息',
                url: '/SystemManage/DataItemDetail/Detail?keyValue=' + keyValue,
                width: "500px",
                height: "480px",
                btn: null
            });
        }
    }
    //保存表单
    function AcceptClick() {
        dialogClose();
    }

</script>
<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
    <div class="ui-layout-center">
        <div class="center-Panel">
            <div class="panel-Title">
                工作类型
            </div>
            <div class="titlePanel">
                <div class="title-search">
                    <table>
                        <tr>
                            <td>
                                <div id="queryCondition" class="btn-group">
                                    <a class="btn btn-default dropdown-text" data-toggle="dropdown">选择条件</a>
                                    <a class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                                    <ul class="dropdown-menu">
                                        <li><a data-value="Name">名称</a></li>
                                        <li><a data-value="Code">编号</a></li>
                                    </ul>
                                </div>
                            </td>
                            <td style="padding-left: 2px;">
                                <input id="txt_Keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;" />
                            </td>
                            <td style="padding-left: 5px;">
                                <a id="btn_Search" class="btn btn-primary"><i class="fa fa-search"></i>&nbsp;查询</a>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="toolbar">
                    <div class="btn-group">
                        <a id="lr-replace" class="btn btn-default" onclick="reload();"><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                        <a id="lr-addRule" class="btn btn-default" onclick="btn_add()"><i class="fa fa-plus"></i>&nbsp;新增</a>
                        <a id="lr-editRule" class="btn btn-default" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>&nbsp;编辑</a>
                        <a id="lr-deleteRule" class="btn btn-default" onclick="btn_delete()"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
                    </div>
                    <script>$('.toolbar').authorizeButton()</script>
                </div>
            </div>
            <div class="gridPanel">
                <table id="gridTable"></table>
                <div id="gridPager"></div>
            </div>
        </div>
    </div>
</div>
